export default class ColorPickerUtil {

    static parseRgbaColor(color: string) {
        const values: any = color
            .replace(/rgba?\(/, "")
            .replace(/\)/, "")
            .replace(/[\s+]/g, "")
            .split(",");

        const a = parseFloat(values[3] || 1);

        return {
            r: Math.floor(a * parseInt(values[0]) + (1 - a) * 255),
            g: Math.floor(a * parseInt(values[1]) + (1 - a) * 255),
            b: Math.floor(a * parseInt(values[2]) + (1 - a) * 255),
            a,
        };
    }

    static parseRgbColor(color: string) {
        const values = color
            .replace(/rgb?\(/, "")
            .replace(/\)/, "")
            .replace(/[\s+]/g, "")
            .split(",");

        const a = 1;

        return {
            r: Math.floor(a * parseInt(values[0]) + (1 - a) * 255),
            g: Math.floor(a * parseInt(values[1]) + (1 - a) * 255),
            b: Math.floor(a * parseInt(values[2]) + (1 - a) * 255),
            a,
        };
    };

    static hex2rgba(s) {
        if (/^#?[0-9a-fA-F]{3}$/.test(s)) {
            const b = s.substring(s.length - 1, s.length);
            const g = s.substring(s.length - 2, s.length - 1);
            const r = s.substring(s.length - 3, s.length - 2);
            return ColorPickerUtil.hex2rgba(`${r + r}${g + g}${b + b}`);
        }
        if (/^#?[0-9a-fA-F]{4}$/.test(s)) {
            const a = s.substring(s.length - 1, s.length);
            const b = s.substring(s.length - 2, s.length - 1);
            const g = s.substring(s.length - 3, s.length - 2);
            const r = s.substring(s.length - 4, s.length - 3);
            return ColorPickerUtil.hex2rgba(`${r + r}${g + g}${b + b}${a + a}`);
        }
        if (/^#?[0-9a-fA-F]{6}$/.test(s)) {
            const b = parseInt("0x" + s.substring(s.length - 2, s.length));
            const g = parseInt("0x" + s.substring(s.length - 4, s.length - 2));
            const r = parseInt("0x" + s.substring(s.length - 6, s.length - 4));
            return { r, g, b, a: 1 };
        }
        if (/^#?[0-9a-fA-F]{8}$/.test(s)) {
            let a = parseInt("0x" + s.substring(s.length - 2, s.length));
            a = a / 255;
            const b = parseInt("0x" + s.substring(s.length - 4, s.length - 2));
            const g = parseInt("0x" + s.substring(s.length - 6, s.length - 4));
            const r = parseInt("0x" + s.substring(s.length - 8, s.length - 6));
            return { r, g, b, a };
        }
    };

    static parseColor(color: string, format: string) {
        if (color) {
            if (format === "hex8") {
                if (
                    /^#?([0-9a-fA-F]{6}|[0-9a-fA-F]{8}|[0-9a-fA-F]{3}|[0-9a-fA-F]{4})$/.test(
                        color
                    )
                ) {
                    return ColorPickerUtil.hex2rgba(color);
                }
            } else {
                return format.toLowerCase().includes("rgba")
                    ? ColorPickerUtil.parseRgbaColor(color)
                    : ColorPickerUtil.parseRgbColor(color);
            }
        } else {
            return null;
        }
    };

}
